<?php

namespace app\fly_pig\controller;

use think\Db;

class FlyPigOrderDetails
{

    public $count = 1;
    /**
     * 构造函数
     * MfwComment constructor.
     */
    public function __construct(){}

    /**
     *  子进程启动时触发
     * @param $worker
     */
    public function onWorkerStart($worker){
        echo "飞猪详情订单子进程启动了\n";
       if ($worker->id === 0){
           \workerman\Lib\Timer::add(30, [$this,'getDate']);
       }elseif ($worker->id === 1){
           \workerman\Lib\Timer::add(100, [$this, 'timeTask']);
       }
    }

    /**
     * 连接时触发
     * @param $connection
     */
    public function onConnect($connection){
        echo "连接成功\n";
        $connection->send('<h2 style="color: #4288ce;font-size: 20px">hello!</h2>');
    }

    /**
     *  接收到消息时触发
     * @param $connection
     * @param $message
     */
    public function onMessage($connection, $message) {
        var_dump($message);
        $connection->send("<span style='color: #000088;'>很高兴和你认识</span>\n");
    }

    /**
     *  连接关闭时触发
     * @param $connection
     */
    public function onClose($connection){
        echo "连接断开了\n";
    }

    /**
     *  worker进程停止时触发
     * @param $connection
     */
    public function onWorkerStop($connection){
        echo "正在重启!\n";
    }


    /**
     *  从奇门平台获取订单信息
     */
    public function timeTask()
    {
        echo "定时任务\n";

        // 根据订单创建日期获取订单数据
        $this->getFlyPigOrderByDate();
        // 根据订单id获取订单
        $this->getOrderId();
        $this->getFlyPigOrderDetail();

    }


    /**
     *  根据时间获取订单详情数据
     */
    public function getFlyPigOrderByDate()
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->select(6);
        $date = $redis->lPop('order_detail_date');
        if (!empty($date)){
            $flyPigObj = new FlyPig();
            $flyPigObj->syncFlyPigDetails('',$date);
        }
    }

    /**
     *  处理30天日期
     */
    public function getDate()
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->select(6);
        // 根据当前时间生成7天之前的日期
        $date = date('Y-m-d',time());
        $dates = $this->getDates($date);
        // 从redis获取数据
        $data1 = $redis->lRange('order_detail_date', 0 , -1);
        $data2 = $redis->lRange('order_detail_date1',0, -1);
        $data = [];
        if (!empty($data2)){
            $data = array_unique(array_merge($data1, $data2));
        }else{
            $data = $data1;
        }
        if (!empty($data)){
            foreach ($dates as $index1 => $datum) {
                if (!in_array($datum, $data)){
                    $redis->rPush('order_detail_date', $datum);
                }
            }
        }else{
            foreach ($dates as $index2 => $datum2) {
                $redis->rPush('order_detail_date', $datum2);
            }
        }
        // 删除列表页多余的日期
        $currentDate = date('Y-m-d', time());
        $st = $currentDate.' 00:00:00';
        $end = $currentDate.' 01:00:00';
        if ($st <= $currentDate && $currentDate <= $end){
            $currentData = $this->getDates($currentDate);
            $orderDate1 = $redis->lRange('order_detail_date1', 0, -1);
            if (!empty($currentData) && !empty($orderDate1)){
                $count = count($orderDate1);
                foreach ($orderDate1 as $index => $item) {
                    if (!in_array($item, $currentData)){
                        $redis->lRem('order_detail_date1',$item,$count);
                    }
                }
            }
        }
    }

    /**
     *  获取前10天的日期
     */
    public function getDates($value = '')
    {
        $arr = [];
        $day = 60;
        $arr[] = $value;
        if (!empty($value)){
            for ($i = 1; $i < $day; $i++) {
                $arr[] = date('Y-m-d', strtotime("{$value} -{$i}day"));
            }
        }
        return $arr;
    }

    /**
     *  获取订单id
     */
    public function getOrderId()
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->select(6);
        $flyPigOrderOldIds = $redis->lRange('fly_pig_order_old_id',0,-1);
        $flyPigOrderIds = $redis->lRange('fly_pig_order_id',0,-1);
        $oid = [];
        if (!empty($flyPigOrderOldIds) && !empty($flyPigOrderIds)){
            $oid = array_unique(array_merge($flyPigOrderIds, $flyPigOrderOldIds));
        }elseif (!empty($flyPigOrderOldIds)){
            $oid = array_unique($flyPigOrderOldIds);
        }elseif (!empty($flyPigOrderIds)){
            $oid = array_unique($flyPigOrderIds);
        }
        $tid = Db::name('fly_pig_order')->where('tid','not in', $oid)
                                                ->field('tid,oid')
                                                ->order('id','desc')
                                                ->limit(2000)
                                                ->select();
        if (!empty($tid)){
            foreach ($tid as $index => $item) {
                $redis->rPush('fly_pig_order_id',$item['tid']);
            }
        }
        // 判断当前的数组大小
        $flyPigOrderLength = count($flyPigOrderOldIds);
        if ($flyPigOrderLength > 3000){
            $flyPigOrderOldIdData = $flyPigOrderOldIds;
            $flyPigOrderOldIdData = array_slice($flyPigOrderOldIdData,0,1000);
            if (!empty($flyPigOrderOldIdData)){
                foreach ($flyPigOrderOldIdData as $index => $flyPigOrderOldIdDatum) {
                    $redis->lRem('fly_pig_order_old_id',$flyPigOrderOldIdDatum,$flyPigOrderLength);
                }
            }
        }
    }

    /**
     *  获取订单详情根据订单id来获取
     */
    public function getFlyPigOrderDetail()
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->select(6);
        $orderId  = $redis->lPop('fly_pig_order_id');
        if (!empty($orderId)){
            $flyPigObj = new FlyPig();
            $flyPigObj->syncFlyPigDetails($orderId,'');
        }
        // 查询订单id是否在订单表
        $find = Db::name('order')->where('order_id', $orderId)->find();
        if ($find){
            $redis->rPush('fly_pig_order_old_id',$orderId);
        }else{
            $redis->rPush('fly_pig_order_id',$orderId);
        }
    }
}